﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatastoreLib;

namespace ElevatorLib
{
    public class ElevatorFeedbackImplementation : ElevatorFeedbackFacade
    {
        #region Declarations

        StorageFacade dbStorage;

        public delegate void TwitterNotificationHandler(object feedback, ElevatorFeedbackEventArgs msg);
        public event TwitterNotificationHandler TwitterNotification;
		
        #endregion

        #region Constructors

        public ElevatorFeedbackImplementation()
        {
            dbStorage = new StorageImplementation();
        }
        #endregion

        #region Delegate Method

        protected void OnTwitterNotification(object feedback, ElevatorFeedbackEventArgs msg)
        {
            if (TwitterNotification != null)
            {
                TwitterNotification(feedback, msg);
            }
            else
            {
                throw new Exception("No listeners to elevator messages found!");
            }
        }

        #endregion
        
        #region ElevatorFeedbackFacade Members

        public void ArrivedAt(int floor)
        {
            dbStorage.StoreArrivedAt(floor);
            ElevatorFeedbackEventArgs message = new ElevatorFeedbackEventArgs("Elevator arrived at floor " + floor.ToString());
            OnTwitterNotification(this, message);
        }

        public void MovingTo(int floor)
        {
            dbStorage.StoreMovingTo(floor);
            ElevatorFeedbackEventArgs message = new ElevatorFeedbackEventArgs("Elevator moving to floor " + floor.ToString());
            OnTwitterNotification(this, message);
        }

        public void NewEmergency()
        {
            dbStorage.StoreNewEmergency();
            ElevatorFeedbackEventArgs message = new ElevatorFeedbackEventArgs("New emergency called from elevator!");
            OnTwitterNotification(this, message);
        }

        public void ResumeOperations()
        {
            dbStorage.StoreResumeOperations();
            ElevatorFeedbackEventArgs message = new ElevatorFeedbackEventArgs("Resuming operations");
            OnTwitterNotification(this, message);
        }


        #endregion
    }
}
